'''
LastEditors: 杜康
LastEditTime: 2021-11-29 16:10:17
'''
# 处理http请求
from urllib.request import urlopen
# 处理http请求错误
from urllib.error import HTTPError
from urllib.error import URLError
# 引入bs4框架
from bs4 import BeautifulSoup
'''
description: 处理获取bs对象中某个节点的可能错误情况
param {*} url
return {*}
'''


def getTitle(url):
    try:
        html = urlopen(url)
    except HTTPError as e:
        return None

    try:
        bs = BeautifulSoup(html.read(), 'html.parser')
        title = bs.body.h1
    # 对象属性错误
    except AttributeError as e:
        return None

    return title


title = getTitle('http://www.pythonscraping.com/pages/page1.html')
if title == None:
    print('无法查询到Title!')
else:
    print(title)
